package crew.blt.handsound.client.mvc.view.crud.user;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

import crew.blt.handsound.client.mvc.model.Model;
import crew.blt.handsound.client.services.user.UserService;
import crew.blt.handsound.client.services.user.UserServiceAsync;
import crew.blt.handsound.client.widgets.user.UserEntityGrid;
import crew.blt.handsound.client.widgets.user.UserPanel;
import crew.blt.handsound.shared.domain.User;
import crew.gwt.client.message.ServicesMessages;
import crew.gwt.client.services.util.ServerCall;
import crew.gwt.client.widgets.DialogPopupPanel;
import crew.gwt.client.widgets.MessagePopupPanel;
import crew.gwt.client.widgets.handler.DialogHandler;
import crew.gwt.client.widgets.handler.OnActionEntityHandler;

public class UserPage<M extends Model> extends IUserPage<M>{

	private final ServicesMessages servicesMessages = GWT.create(ServicesMessages.class);
	private final UserServiceAsync entityService = GWT.create(UserService.class);

	@UiTemplate("UserPage.ui.xml")
	interface MPPageUIBinder extends UiBinder<Widget, UserPage> {}
	private static MPPageUIBinder uiBinder = GWT.create(MPPageUIBinder.class);

	//Attributes
	@UiField UserEntityGrid grid;
	@UiField UserPanel panel;
	
	/**
	 * Constructor
	 */
	public UserPage() {
	    initWidget(uiBinder.createAndBindUi(this));
	    grid.setOnSelectHandler(onSelectMPHandler);
	}
	
	@Override
	public Widget getWidgetPage() {
		return this;
	}

	@Override
	public void setUser(User user) {
	}


	//---------------------------------------------------------------------
	//-------------------------- UI action

	
	//---------------------------------------------------------------------
	//-------------------------- Handler

	private OnActionEntityHandler<User> onSelectMPHandler = new OnActionEntityHandler<User>() {
		@Override
		public void onSelectEntity(User art) {
			panel.setEntity(art.getCopy());
		}

		@Override
		public void onDeleteEntity(User t) {
			deleteEntity(t);
		}

		@Override
		public void onEditEntity(User t) {
			final UserPanel panel = new UserPanel();
			panel.setEditMode(true);
			panel.setEntity(t);
			new DialogPopupPanel(panel, new DialogHandler() {
				@Override
				public void onConfirmClick() {
					panel.applyChangeOnEntity();
					editEntity(panel.getEntity());	
				}
				@Override
				public void onCancelClick() {
				}
			});
		}
	};
	
	//---------------------------------------------------------------------
	//-------------------------- Server call
	
	private void deleteEntity(final User t) {
		if(t.getId() == null || t.getId().isEmpty()){
			new MessagePopupPanel(false,servicesMessages.operationFailedHeader(),servicesMessages.formInvalid()).show();
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			public void onFailure(Throwable t) {
				new MessagePopupPanel(false,servicesMessages.operationFailedHeader(),"La matière première est peut être associé à une réception").show();
			};
			
			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				entityService.deleteUser(t.getId(),asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				if(success){
					grid.removeEntity(t);
					new MessagePopupPanel(true,servicesMessages.operationSuccessHeader(),servicesMessages.operationRemoveSuccess("La matière première ")).show();
				}
				else
					new MessagePopupPanel(false,servicesMessages.operationFailedHeader(),"La matière première est peut être associé à une réception").show();
			}
		}.call();
	}
	
	private void editEntity(final User entity) {
		if(!checkFormValidity(entity)){
			new MessagePopupPanel(false,servicesMessages.operationFailedHeader(),servicesMessages.formInvalid()).show();
			return;
		}
		new ServerCall<Boolean>() {

			@Override
			protected void callService(AsyncCallback<Boolean> asyncCallback) {
				entityService.editUser(entity,asyncCallback);
			}

			@Override
			public void onSuccess(Boolean success) {
				if(success){
					grid.editEntity(entity);
					new MessagePopupPanel(true,servicesMessages.operationSuccessHeader(),servicesMessages.operationEditSuccess("La réception ")).show();
				} else
					new MessagePopupPanel(false,servicesMessages.operationFailedHeader(),servicesMessages.operationFailed()).show();
			}
		}.call();
	}
	
	private boolean checkFormValidity(User e) {
		if(e.getId() == null || e.getId().isEmpty()) return false;
		return true;
	}
}
